package com.sino.config.server.controller.project;

import org.nutz.dao.Cnd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sino.config.server.ConfigServerApplication.Consts.InstalledPermission;
import com.sino.config.server.bean.project.Label;
import com.sino.config.server.controller.BaseController;
import com.sino.config.server.ext.anno.SINORequiresPermissions;
import com.sino.config.server.service.project.LabelService;

import club.zhcs.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @author kerbores
 *
 */
@RestController
@RequestMapping("label")
@Api(value = "label", tags = { "label" })
public class LabelController extends BaseController {

	@Autowired
	LabelService labelService;

	/**
	 * 
	 * @param page
	 * @return
	 */
	@GetMapping("list")
	@SINORequiresPermissions(InstalledPermission.LABEL_LIST)
	@ApiOperation("label列表")
	public Result list(@RequestParam(value = "page", defaultValue = "1") @ApiParam("页码") int page) {
		return Result.success().addData("pager", labelService.searchByPage(_fixPage(page), Cnd.NEW().asc("weight")));
	}

	/**
	 * 
	 * @param key
	 * @param page
	 * @return
	 */
	@GetMapping("search")
	@SINORequiresPermissions(InstalledPermission.LABEL_LIST)
	@ApiOperation("label分页搜索")
	public Result search(@RequestParam("key") String key, @RequestParam(value = "page", defaultValue = "1") int page) {
		return Result.success().addData("pager", labelService.searchByKeyAndPage(_fixSearchKey(key), _fixPage(page), "name", "description").addParam("key", key));
	}

	/**
	 * 
	 * @param label
	 * @return
	 */
	@PostMapping("add")
	@SINORequiresPermissions(InstalledPermission.LABEL_ADD)
	@ApiOperation("label新增")
	public Result save(@RequestBody Label label) {
		return labelService.save(label) == null ? Result.fail("保存label失败!") : Result.success().addData("label", label);
	}

	/**
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping("{id}")
	@SINORequiresPermissions(value = { InstalledPermission.LABEL_EDIT })
	@ApiOperation("label详情")
	public Result detail(@PathVariable("id") @ApiParam("labelid") long id) {
		return Result.success().addData("label", labelService.fetch(id));
	}

	/**
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping("delete/{id}")
	@SINORequiresPermissions(value = { InstalledPermission.LABEL_DELETE })
	@ApiOperation("删除label")
	public Result delete(@PathVariable("id") @ApiParam("Labeld") long id) {
		return labelService.delete(id) == 1 ? Result.success() : Result.fail("删除label失败!");
	}

	/**
	 * 
	 * @param label
	 * @return
	 */
	@PostMapping("edit")
	@SINORequiresPermissions(InstalledPermission.LABEL_EDIT)
	@ApiOperation("编辑label")
	public Result update(@RequestBody Label label) {
		return labelService.updateIgnoreNull(label) != 1 ? Result.fail("更新label失败!") : Result.success().addData("label", label);
	}
}
